#include <iostream>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>

#include "asset.hpp"

void load_mesh(const char * path, Eigen::MatrixX3f & vertices, Eigen::MatrixX3i & faces)
{
    Assimp::Importer importer;
    const aiScene * scene = importer.ReadFile(path, aiProcess_Triangulate);
    if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE)
    {
        std::cerr << "Assimp::Importer::ReadFile error: " << importer.GetErrorString() << "\n";
        exit(1);
    }

    aiMesh * mesh = scene->mMeshes[0];

    vertices.resize(mesh->mNumVertices, 3);

    for (unsigned i = 0; i < mesh->mNumVertices; i++)
        vertices.row(i) <<
            mesh->mVertices[i].x,
            mesh->mVertices[i].y,
            mesh->mVertices[i].z;

    faces.resize(mesh->mNumFaces, 3);

    for (unsigned i = 0; i < mesh->mNumFaces; i++)
        faces.row(i) <<
            mesh->mFaces[i].mIndices[0],
            mesh->mFaces[i].mIndices[1],
            mesh->mFaces[i].mIndices[2];
}
